package com.industry.framework.core.generic.record.dataobject.entity;

import cn.dev33.satoken.stp.StpUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.industry.framework.core.generic.record.Record;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDateTime;

/**
 * @Author nbZhuozh
 * @Date 2025-03-26  15:48
 **/
@Data
public class RecordEntity {

    RecordEntity(String bizTable,String bizId,String eventType,String newValue ,String createUser){
        this.bizTable = bizTable;
        this.bizId = bizId;
        this.eventType = eventType;
        this.newValue = newValue;
        this.createUser = createUser;
        this.createTime = LocalDateTime.now();
    }
    RecordEntity(String bizTable,String bizId,String eventType,String createUser){
        this.bizTable = bizTable;
        this.bizId = bizId;
        this.eventType = eventType;
        this.createUser = createUser;
        this.createTime = LocalDateTime.now();
    }
    RecordEntity(String bizTable,String bizId,String eventType,String createUser,String filed, String filedName, String oldValue, String newValue){
        this.bizTable = bizTable;
        this.bizId = bizId;
        this.eventType = eventType;
        this.createUser = createUser;
        this.createTime = LocalDateTime.now();
        this.filed = filed;
        this.filedName = filedName;
        this.newValue = newValue;
        this.oldValue = oldValue;
    }

    //template 更新操作 :  【操作日期】 ： 【操作人】 更新 【字段名】 【旧值】 -> 【新值】
    //template 新增操作 :  【操作日期】 ： 【操作人】 新建
    //template 上传操作 :  【操作日期】 ： 【操作人】 上传附件 【文件名】【文件类型】
    private String id;

    private String filed; //更新的字段

    public RecordEntity(String id, String filed, String filedName, String oldValue, String newValue, String eventType, String bizTable, String bizId, LocalDateTime createTime, String createUser) {
        this.id = id;
        this.filed = filed;
        this.filedName = filedName;
        this.oldValue = oldValue;
        this.newValue = newValue;
        this.eventType = eventType;
        this.bizTable = bizTable;
        this.bizId = bizId;
        this.createTime = createTime;
        this.createUser = createUser;
    }

    private String filedName; //字段名称

    private String oldValue; //旧值

    private String newValue; //新值

    private String eventType; //事件类型 : 新建、更新、上传

    private String bizTable; //业务表

    private String bizId; //业务表ID
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createTime; //创建时间

    private String createUser; //创建人

    public RecordEntity() {

    }


    //新增类型构造
    public static RecordEntity ofTypeInsert(String bizTable , String bizId){
        return new RecordEntity(bizTable,bizId, Record.INSERT, String.valueOf(StpUtil.getLoginId()));
    }
    //修改类型构造
    public static RecordEntity ofTypeUpdate(String bizTable,String bizId , String filedStr ,String filedNameStr , String newValue , String oldValue ){
        if(StringUtils.isBlank(newValue)){
            newValue = "无";
        }
        if(StringUtils.isBlank(oldValue)){
            oldValue = "无";
        }
        if(StringUtils.isBlank(filedNameStr)){
            filedNameStr = "未知字段";
        }
        return  new RecordEntity(bizTable,bizId,Record.UPDATE,String.valueOf(StpUtil.getLoginId()),filedStr,filedNameStr,newValue,oldValue);
    }
    //备注类型构造
    public static RecordEntity ofTypeRemark(String bizTable,String bizId , String content){
        return new RecordEntity(bizTable,bizId, Record.REMARK, content, String.valueOf(StpUtil.getLoginId()));
    }
}
